import http from '../../utils/httpRequest'
import {treeDataTranslate} from '@/utils'

export default {
  namespaced: true,
  state: {
    id: 0,
    name: '',
    /* 人员字典字段 */
    professionalTitleList: [],
    educationList: [],
    certTypeList: [],
    dutyList: [],
    politicCountenanceList: [],
    punishList: [],
    /* ------------ */
    deptTree: null,   // 部门树
    deptList: [],   // 部门
    craftList: [],   // 工种
    alertListCount: null,
    alertList: []
  },
  getters: {
    professionalTitleList: state => state.professionalTitleList,
    educationList: state => state.educationList,
    certTypeList: state => state.certTypeList,
    dutyList: state => state.dutyList,
    politicCountenanceList: state => state.politicCountenanceList,
    punishList: state => state.punishList,
    deptTree: state => state.deptTree,
    deptList: state => state.deptList,
    craftList: state => state.craftList,
    alertListCount: state => state.alertListCount
  },
  mutations: {
    updateId (state, id) {
      state.id = id
    },
    updateName (state, name) {
      state.name = name
    },
    updateProfessionalTitleList (state, list) {
      state.professionalTitleList = list
    },
    updateEducationList (state, list) {
      state.educationList = list
    },
    updateCertTypeList (state, list) {
      state.certTypeList = list
    },
    updateDutyList (state, list) {
      state.dutyList = list
    },
    updatePoliticCountenanceList (state, list) {
      state.politicCountenanceList = list
    },
    updatePunishList (state, list) {
      state.punishList = list
    },
    updateDeptTree (state, tree) {
      state.deptTree = tree
    },
    updateDeptList (state, list) {
      state.deptList = list
    },
    updateCraftList (state, list) {
      state.craftList = list
    },
    updateAlertListCount (state, count) {
      state.alertListCount = count
    }
  },
  actions: {
    async loadUserDict_4 ({commit}) {
      let res = await http({
        method: 'post',
        url: '/usercenter-admin/dict/getDictItemGroupByTypeList'
      })
      commit('updateProfessionalTitleList', res.data.professionalTitleList)
      commit('updateDutyList', res.data.dutyList)
      commit('updateEducationList', res.data.educationList)
      commit('updatePoliticCountenanceList', res.data.politicCountenanceList)
    },
    async loadProfessionalTitleList ({commit}) {
      let res = await http({
        method: 'post',
        url: '/usercenter-admin/dict/type',
        data: {type: 'PROFESSIONAL_TITLE'}
      })
      commit('updateProfessionalTitleList', res.data)
    },
    async loadDutyList ({commit}) {
      let res = await http({
        method: 'post',
        url: '/usercenter-admin/dict/type',
        data: {type: 'DUTY'}
      })
      commit('updateDutyList', res.data)
    },
    async loadEducationList ({commit}) {
      let res = await http({
        method: 'post',
        url: '/usercenter-admin/dict/type',
        data: {type: 'EDUCATION'}
      })
      commit('updateEducationList', res.data)
    },
    async loadPoliticCountenanceList ({commit}) {
      let res = await http({
        method: 'post',
        url: '/usercenter-admin/dict/type',
        data: {type: 'POLITIC_COUNTENANCE'}
      })
      commit('updatePoliticCountenanceList', res.data)
    },
    async loadCertTypeList ({commit}) {
      let res = await http({
        method: 'post',
        url: '/usercenter-admin/dict/type',
        data: {type: 'CERTTYPE'}
      })
      commit('updateCertTypeList', res.data)
    },
    async loadPunishList ({commit}) {
      let res = await http({
        method: 'post',
        url: '/usercenter-admin/dict/type',
        data: {type: 'PUNISH'}
      })
      commit('updatePunishList', res.data)
    },
    async loadDeptList ({commit}) {
      let res = await http({
        url: '/usercenter-admin/dept/list',
        method: 'post'
      })
      commit('updateDeptList', res.data)

      let temp = JSON.parse(JSON.stringify(res.data))
      let tree = treeDataTranslate(temp, 'id')
      commit('updateDeptTree', tree)
    },
    async loadCraftList ({commit}) {
      let res = await http({
        method: 'post',
        url: '/usercenter-admin/user/craft/pageList'
      })
      let sorted = res.data.sort((a, b) => b.id - a.id)
      commit('updateCraftList', sorted)
    },
    async loadAlertListCount ({commit}) {
      let res = await http({
        url: '/usercenter-admin/certConfig/getAlertListCount',
        method: 'post'
      })
      commit('updateAlertListCount', res.data)
    }
  }
}
